library(dplyr)

## set working directory
## to Dataverse folder

survey <- read.table("Harris_Data/Harris 1991 Public Opinion Survey, study no. 911108/harris_s911108_spss.tab", header = TRUE)

# pid
survey$pid <- c(1:nrow(survey))

# study
survey$study <-"911108"

# study year (year)
survey$year <- 1991

# geographic data (urban)
survey$urban <- NA

# geographic data (region)
survey$region <- NA

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
summary(survey$Q2_C)
survey$inequality <- dplyr::recode(as.character(survey$Q2_C),
                            `1` = "Feel",
                            `2` = "Don't Feel",
                            `3` = "Not Sure")
table(survey$inequality)
table(survey$Q2_C)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
table(survey$F3_1)
survey$union.self <- dplyr::recode(survey$F3_1,
                            `0` = "No",
                            `1` = "Yes")
table(survey$union.self)

survey$union.other <- dplyr::recode(survey$F3_2,
                             `0` = "No",
                             `1` = "Yes")
table(survey$union.other)
table(survey$F3_4) # not sure
survey[survey$F3_4 == 1, c("union.self", "union.other", "F3_3")]

survey$union.self[survey$F3_4 == 1] <- "Not Sure"
survey$union.other[survey$F3_4 == 1] <- "Not Sure"

table(survey$union.self)
table(survey$union.other)

# employment (employed)
survey$employed <- NA

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- NA

## occ self
survey$occupation.self <- NA

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
table(survey$F2)
survey$educ <- dplyr::recode(survey$F2,
                      `1` = "Less than high school",
                      `2` = "High school graduate",
                      `3` = "Some college",
                      `4` = "College graduate",
                      `5` = "Post graduate",
                      `6` = "Not sure")
table(survey$educ)

# household income (income)
survey$income <- dplyr::recode(survey$F8,
                        `1` = "7,500 or less",
                        `2` = "7,501 to 15,000",
                        `3` = "15,001 to 25,000",
                        `4` = "25,001 to 35,000",
                        `5` = "35,001 to 50,000",
                        `6` = "50,001 to 75,000",
                        `7` = "75,001 to 100,000",
                        `8` = "100,001 and over",
                        `9` = "Not sure")
table(survey$income)

# age
survey$age <- dplyr::recode(survey$F1,
                     `1` = "18 to 20",
                     `2` = "21 to 24",
                     `3` = "25 to 29",
                     `4` = "30 to 34",
                     `5` = "35 to 39",
                     `6` = "40 to 44",
                     `7` = "45 to 49",
                     `8` = "50 to 64",
                     `9` = "65 to 74",
                     `10` = "75 and over",
                     `11` = "Not sure") # in codebook as x, but values of F2 variable suggest otherwise
table(survey$age)

# race
survey$race1 <- dplyr::recode(survey$F10,
                      `1` = "White",
                      `2` = "Black",
                      `3` = "Asian or Pacific Islander",
                      `4` = "American Indian or Alaskan native",
                      `5` = "Not sure")
survey$race2 <- dplyr::recode(survey$F9,
                              `3` = "Decline/not sure",
                              `1` = "Yes, hispanic",
                              `2` = "No, not hispanic")
table(survey$race1)
table(survey$race2)
sum(is.na(survey$race1))
survey$race <- ifelse(survey$race1 == "Not sure", 
                      "Decline/not sure", ifelse(survey$race1 == "White",
                                                 ifelse(survey$race2 == "Decline/not sure",
                                                        "Decline/not sure",
                                                        ifelse(survey$race2 == "No, not hispanic",
                                                               "Non-Hispanic White",
                                                               "Hispanic White")), "Non-white"))
table(survey$race)
table(survey$race[survey$race1 == "White"])

# politics (party)
survey$party <- dplyr::recode(survey$Q5A,
                       `1` = "Republican",
                       `2` = "Democrat",
                       `3` = "Independent",
                       `4` = "Other (vol.)",
                       `5` = "Not sure")
table(survey$party)

# politics (ideology)
survey$ideology <- dplyr::recode(survey$F5,
                          `1` = "Conservative",
                          `2` = "Moderate",
                          `3` = "Liberal",
                          `4` = "Not sure")
table(survey$ideology)

# gender, "original data"
colnames(survey)
summary(survey$Q1)
survey$gender <- (dplyr::recode(survey$Q1,
                               `1` = "Male",
                               `2` = "Female"))
table(survey$gender)

# religion
survey$religion <- dplyr::recode(survey$F4A,
                          `1` = "Protestant",
                          `2` = "Catholic",
                          `3` = "Jewish",
                          `4` = "Other",
                          `5` = "None (vol.)",
                          `6` = "Not sure")
table(survey$religion)

#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- NA
survey$dontcount <- NA
survey$leftout <- NA

## question place
survey$question_place <- "before party"

# subset
survey_911108 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                           "inequality", "inequality.variable", "union.self", "union.other",
                           "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                           "age", "race", "party", "ideology", "gender", "religion",
                           "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                           "question_place")]

# save file
#saveRDS(survey_911108, file = "Harris_Data/survey_911108.rds")
